package com.kanas.webak.admin.controller;

import java.util.HashMap;
import java.util.List;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;


import com.kanas.framework.business.BusinessHandler;
import com.kanas.framework.business.ResponseMessage;
import com.kanas.framework.support.ControllerSupport;
import com.kanas.webak.admin.domain.AppUser;
import com.kanas.webak.admin.service.UserService;
import com.kanas.webak.organization.domain.Organization;
import com.kanas.webak.organization.service.OrganizationService;
import com.kanas.webak.rbac.domain.AppUserRole;

@Controller
@RequestMapping("/app/adminuser")
public class SysUserController extends ControllerSupport {
	
	private UserService userService;
	private OrganizationService orgService;

	@RequestMapping(value="", method=RequestMethod.GET)
	public ModelAndView role(final HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		mv.setViewName("modules/sysadmin/user");
		List<Organization> orgs = orgService.findAll();
		mv.addObject("orgs", orgs);
		return mv;
	}

	@RequestMapping(value="/user",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> listAll(@RequestParam(value="start",required=false)Integer page,@RequestParam(value="count",required=false)Integer rows){
		Map<String,Object> response=new HashMap<String,Object>();
		PageRequest pageObject = new PageRequest(page, rows);
		response.put("identifier", "id");
		response.put("label", "label");
		response.put("items", this.getUserService().findAll(pageObject).getContent());
		response.put("numRows", this.getUserService().count());
		return response;
	}
	
	@RequestMapping(value="edit/{userid}.html",method=RequestMethod.GET)
	public ModelAndView editView(@PathVariable("userid")String userId){
		ModelAndView mv = new ModelAndView();
		mv.setViewName("modules/sysadmin/user_edit");
		mv.addObject("user",this.userService.findOne(userId));
		return mv;
	}
	
	@RequestMapping(value="rpassword/{userid}",method=RequestMethod.PUT)
	public @ResponseBody String resetPassword(@PathVariable("userid")final String userId){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				userService.resetPassword(userId);
				return null;
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/user",method=RequestMethod.POST)
	public @ResponseBody ResponseMessage addNew(@Valid final AppUser user, BindingResult result,
			@RequestParam("roleIds") final String roleIds) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				AppUser au=user;
				return userService.addNew(au,roleIds);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="/user/{userid}",method=RequestMethod.DELETE)
	public @ResponseBody String delete(@PathVariable("userid") final String userId){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return userService.delete(userId);
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/user/{userId}",method=RequestMethod.POST)
	public @ResponseBody String updata(@PathVariable("userId")final String userId,@RequestParam("roleIds") final String roleIds,final AppUser user){
		ResponseMessage rm = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				userService.update(userId, user, roleIds);
				return null;
			}
		}, logger);
		return rm.toString();
	}
	
	@RequestMapping(value="/changepsd.do",method=RequestMethod.POST)
	public @ResponseBody ResponseMessage changePsd(@RequestParam(value = "oldPsd", required = false) final String oldPsd,
													@RequestParam(value = "newPsd", required = false)final String newPsd) {
		ResponseMessage response = ResponseMessage.handleException(new BusinessHandler(){
			@Override
			public Object handle() throws Exception {
				return userService.changePsd(oldPsd, newPsd);
			}
		}, logger);
		return response;
	}
	
	@RequestMapping(value="user/userRole",method=RequestMethod.GET)
	public @ResponseBody Map<String,Object> userRole(@RequestParam(value="id",required=false) final String id,
			@RequestParam(value="orgid",required=false) final String orgId) throws Exception{
		Map<String,Object> response=new HashMap<String,Object>();
		List<AppUserRole> aus = userService.findUserRoles(id, orgId);
		response.put("identifier", "id");
		response.put("label", "roleLabel");
		response.put("items", aus);
		return response;
	}
	
	public UserService getUserService() {
		return userService;
	}
	
	@Resource(name="kanas.admin.service.sysuserservice")
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	
	public OrganizationService getOrgService() {
		return orgService;
	}

	@Resource(name="kanas.admin.service.organization")
	public void setOrgService(OrganizationService orgService) {
		this.orgService = orgService;
	}
}